//
//  LuckyWheelView.swift
//  FromWatchToCircle
//
//  Created by 张海峰 on 2018/6/8.
//  Copyright © 2018年 张海峰. All rights reserved.
//
/*写该demo的目的：主要是想从时钟开始，一步步窥见项目中和圆有关的东西，从而学习巩固，加深理解。
 (项目不定时更新，所以最好从下面地址获取最新demo),感觉我这个demo对你有启发或者帮助，欢迎fork 感谢star
 https://github.com/FighterLightning/FromWatchToCircle.git
 */
/* 星座转盘，类似幸运转盘，抽奖等*/
import UIKit

class LuckyWheelView: UIView,UIGestureRecognizerDelegate {
    var imageView: UIImageView = UIImageView()
    var panWidth: CGFloat = 0.0
    var panHeight: CGFloat = 0.0
    var selectBtn: MyButton = MyButton()
    var link:CADisplayLink? = nil
    //初始化视图
    func initLuckyWheelView() -> UIView{
        self.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
        //背景区域的颜色和透明度
        self.backgroundColor = ZHFColor.white.withAlphaComponent(1)
        self.addContentView()
        let tap: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapClick))
        tap.delegate = self
        self.addGestureRecognizer(tap)
        return self
    }
    //添加内容视图
    func addContentView(){
        //添加圆盘
        let image :UIImage = UIImage.init(named: "LuckyRotateWheel")!
        imageView = UIImageView.init(image: image)
        panWidth = image.size.width
        panHeight = image.size.height
        imageView.center = self.center
        imageView.isUserInteractionEnabled = true
        self.addSubview(imageView)
        //添加指针
        let pointerView :UIView = UIView.init(frame: CGRect.init(x: 0, y: self.frame.size.height/2, width: self.frame.size.width/2 - panWidth/4, height: 2))
        pointerView.backgroundColor = ZHFColor.red
        self.addSubview(pointerView)
        //添加星座按钮
        for i in 0 ..< 12 {
            let btn: MyButton = MyButton.init(type: UIButtonType.custom)
            btn.setBackgroundImage(UIImage.init(named: "LuckyRototeSelected"), for: UIControlState.selected)
            btn.frame = CGRect.init(x: 0, y: 0, width: panWidth/5, height:panHeight/2)//这个宽/5是大致算出来的，你可以调成其它看效果图
            btn.tag = i
            btn.layer.position = CGPoint.init(x: panWidth/2, y: panHeight/2)
            //绕中心转
            btn.layer.anchorPoint = CGPoint.init(x: 0.5, y: 1)
            btn.transform = CGAffineTransform.init(rotationAngle: .pi/6 * CGFloat(i))
            btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)
            imageView.addSubview(btn)
            if i == 0{
                self.btnClick(btn: btn)
            }
            //剪切星座长图
            let normalImage: UIImage = UIImage.init(named: "LuckyAstrology")!
            let minW :CGFloat = normalImage.size.width/12 * 2 //因为没3X图[UIScreen mainScreen].scale 用2统一
            let minH :CGFloat = normalImage.size.height * 2 //因为3X图[UIScreen mainScreen].scale 用2统一
            let minRect: CGRect = CGRect.init(x:CGFloat(i) * minW, y: 0, width: minW, height: minH)
            //剪切
            let newCGImage = normalImage.cgImage!.cropping(to: minRect)
            btn.setImage(UIImage.init(cgImage: newCGImage!), for: UIControlState.normal)
            let selectImage: UIImage = UIImage.init(named: "LuckyAstrologyPressed")!
            let minW1 :CGFloat = selectImage.size.width/12 * 2 //因为没3X图[UIScreen mainScreen].scale 用2统一
            let minH1 :CGFloat = selectImage.size.height * 2 //因为3X图[UIScreen mainScreen].scale 用2统一
            let minRect1: CGRect = CGRect.init(x:CGFloat(i) * minW1, y: 0, width: minW1, height: minH1)
            //剪切
            let newCGImage1 = selectImage.cgImage!.cropping(to: minRect1)
            btn.setImage(UIImage.init(cgImage: newCGImage1!), for: UIControlState.selected)
        }
        //添加开始按钮
        let centerImage : UIImage = UIImage.init(named: "LuckyCenterButton")!
        let centerImage1 : UIImage = UIImage.init(named: "LuckyCenterButtonPressed")!
        let centerBtn = UIButton.init(type: UIButtonType.custom)
        centerBtn.frame = CGRect.init(x: 0, y: 0, width: centerImage.size.width, height:centerImage.size.height)
        centerBtn.center = imageView.center
        centerBtn.setImage(centerImage, for: UIControlState.normal)
        centerBtn.setImage(centerImage1, for: UIControlState.highlighted)
        centerBtn.addTarget(self, action: #selector(centerBtnClick), for: UIControlEvents.touchUpInside)
        self.addSubview(centerBtn)
    }
    @objc func btnClick(btn: MyButton){
        if selectBtn == btn {
            btn.isSelected = !btn.isSelected
        }
        else{
            selectBtn.isSelected = false
            btn.isSelected = true
        }
        selectBtn = btn
    }
    @objc func centerBtnClick(){
        if  link == nil {
            link = CADisplayLink.init(target: self, selector: #selector(move))
            link?.add(to: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
        }else{
            if link?.isPaused == true{
                link?.isPaused = false
            }
            else{
                link?.isPaused = true
            }
        }
    }
    @objc func move(){
        imageView.transform = imageView.transform.rotated(by: .pi/180 * 10)
    }
    @objc func tapClick(){
        self.removeFromSuperview()
    }
}
